package com.gccloud.design;


import com.gccloud.design.dto.SdOptBranchDto;
import com.gccloud.design.entity.SdOptBranchEntity;
import com.gccloud.design.service.SdOptBranchService;
import com.gccloud.design.vo.SdOptBranchVo;
import com.gccloud.starter.common.utils.BeanConvertUtils;
import com.gccloud.starter.common.vo.R;
import com.gccloud.starter.core.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author wu.yumin
 * @version 1.0.0
 * @Description 流程环节操作配置
 * @date 2021/7/19 10:05
 */
@RestController
@RequestMapping("/branch")
@Api(tags ="流程环节操作配置")
public class SdOptBranchController {

    @Resource
    private SdOptBranchService sdOptBranchService;

    @ApiOperation(value = "分页查询操作分支列表",produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/pageList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页码", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", paramType = "query", required = true, dataType = "int"),
            @ApiImplicitParam(name = "searchKey", value = "查询条件", paramType = "query", dataType = "string")
    })
    public R<PageVO<SdOptBranchVo>> queryPageList(SdOptBranchDto searchDTO){
        PageVO<SdOptBranchEntity> pageList = sdOptBranchService.queryPageList(searchDTO);
        PageVO pageVo = BeanConvertUtils.convert(pageList, PageVO.class);
        pageVo.setList(pageList.getList());
        return R.success(pageVo);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增操作分支",produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> add(@RequestBody SdOptBranchVo sdOptBranchVo){
        sdOptBranchService.addOptBranch(BeanConvertUtils.convert(sdOptBranchVo, SdOptBranchEntity.class));
        return R.success();
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改操作分支",produces = MediaType.APPLICATION_JSON_VALUE)
    public R<String> update(@RequestBody SdOptBranchVo sdOptBranchVo){
        sdOptBranchService.updateOptBranch(BeanConvertUtils.convert(sdOptBranchVo, SdOptBranchEntity.class));
        return R.success();
    }

    @PostMapping("/del/{ids}")
    @ApiOperation("删除操作分支")
    @ApiImplicitParam(name = "ids", value = "操作分支Id数组", required = true,allowableValues = "true", dataType = "Long", paramType = "path")
    public R<Object> delete(@PathVariable("ids") Long[] ids){
        sdOptBranchService.deleteOptBranch(ids);
        return R.success();
    };

    @GetMapping("/get/{branchCode}")
    @ApiOperation("根据操作分支编码查询操作分支")
    @ApiImplicitParam(name = "branchCode", value = "操作分支编码", required = true,dataType = "String", paramType = "path")
    public R<SdOptBranchVo> delete(@PathVariable("branchCode") String branchCode){
        SdOptBranchEntity entity = sdOptBranchService.getByCode(branchCode);
        if(entity == null){
            return R.success();
        }
        SdOptBranchVo convert = BeanConvertUtils.convert(entity, SdOptBranchVo.class);
        return R.success(convert);
    };
}
